package com.ruihang.generalibrary.db;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.utils.ResultAsyncTask;

import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by panxd on 2016/9/26.
 */
public class CacheUtils {
    public static <T extends ILocalDB> void cacheForResultAsyc(List<T> list, final ICacheCallBack<T> cacheCallBack){
        if(list==null || list.size()==0){
            return;
        }
        new DataFreshAsyncTask<List<T>>(list) {
            @Override
            protected void dataFresh() {

                for (int i = 0; i < data.size(); i++) {
                    T t = data.get(i);
                    try {
                        mappingLocal(t);
                        if(cacheCallBack!=null){
                            cacheCallBack.anotherFresh(t);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                try {
                    cache(data);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onPostExecute(List<T> ts) {
                if(cacheCallBack!=null){
                    cacheCallBack.onCacheListFinished(ts);
                }
            }
        }.ex();
    }

    public static <T extends ILocalDB> void mappingLocal(T t) throws Exception {
            T tt= (T) DBUtils.getInstance().getDbManager().findById(t.getClass(),t.getKey());
//        JLog.e("t ="+t);
            if(tt!=null){
//                JLog.e("t ="+t+"\ntt="+tt);
//                JLog.e("tt="+tt);
                t.setCollectData(tt.getCollectData());
                t.setHistory(tt.getHistoryTime());
                t.initLocal(tt);
            }else{
                t.setHistory(null);
                t.setCollectData(null);
            }
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
    }

//    public static <T extends ILocalDB> void refresh(List<T> data){
//        for (int i = 0; i < data.size(); i++) {
//            T t=data.get(i);
//            try {
//                T tt= (T) DBUtils.getInstance().getDbManager().findById(t.getClass(),t.getKey());
//                if(tt!=null){
//                    t.setCollectData(tt.getCollectData());
//                    t.setHistory(tt.getHistoryTime());
//                    t.initLocal(tt);
//                }else{
//                    t.setHistory(null);
//                    t.setCollectData(null);
//                }
//
//            } catch (DbException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    public static <T> void cache(List<T> list) throws DbException {
        if(list==null || list.size()==0){
            return;
        }
        DBUtils.getInstance().getDbManager().replace(list);
    }


    public static <T> void cache(T t) throws DbException {
        if(t==null ){
            return;
        }


        DBUtils.getInstance().getDbManager().replace(t);
    }

    public static <T> void cacheAsync(final List<T> list){
        if(list==null || list.size()==0){
            return;
        }
        ResultAsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    cache(list);
                } catch (DbException e) {
                    e.printStackTrace();
                    JLog.e(list.get(0).getClass().getName());
                }
            }
        });
    }

    public static <T> void cacheAsync(final T t){
        if(t==null){
            return;
        }
        ResultAsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    cache(t);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public static <T> void cacheAsynAdinfo(final List<T> list){
        if(list==null || list.size()==0){
            return;
        }
        ResultAsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    JLog.e("---info--"+list.toString());
                    cache(list);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public interface ICacheCallBack<E>{
        void onCacheListFinished(List<E> eList);
        void onCacheObjFinished(E e);
        void anotherFresh(E e);
    }

    public static abstract class CacheListCallBack<E> implements ICacheCallBack<E>{
        @Override
        public void onCacheObjFinished(E e) {
        }

        @Override
        public void anotherFresh(E e) {
        }
    }

    public static abstract class CacheObjCallBack<E> implements ICacheCallBack<E>{
        @Override
        public void onCacheListFinished(List<E> eList) {
        }

        @Override
        public void anotherFresh(E e) {
        }
    }

    public static abstract class CacheAnotherCallBack<E> implements ICacheCallBack<E>{
        @Override
        public void onCacheListFinished(List<E> eList) {
        }

        @Override
        public void onCacheObjFinished(E e) {
        }
    }


}
